package com.xxl.job.core.commons.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author xuyue
 * @email xuyue@cetcbigdata.com
 * @date 2021-4-20 10:58
 * Controller统一返回结构
 */
@Data
public class ReturnT<T> {

    private String msg = ReturnCode.SUCCESS.getMsg();
    private Integer code = ReturnCode.SUCCESS.getCode();
    private T data;

    public static ReturnT ok() {
        return new ReturnT<>();
    }

    public static boolean isOk(ReturnT returnT) {
        return returnT.getCode().equals(ReturnCode.SUCCESS.getCode());
    }

    public boolean isOk() {
        return this.code.equals(ReturnCode.SUCCESS.getCode());
    }


    public static ReturnT error(ReturnCode returnCode) {
        ReturnT returnT = new ReturnT<>();
        returnT.setMsg(returnCode.getMsg());
        returnT.setCode(returnCode.getCode());
        return returnT;
    }

    public static ReturnT error() {
        return error(ReturnCode.ERROR);
    }

    public static ReturnT error(ReturnCode returnCode, String msg) {
        ReturnT returnT = new ReturnT<>();
        returnT.setMsg(msg);
        returnT.setCode(returnCode.getCode());
        return returnT;
    }

    public static ReturnT error(Integer code, String msg) {
        ReturnT returnT = new ReturnT<>();
        returnT.setMsg(msg);
        returnT.setCode(code);
        return returnT;
    }

    public ReturnT put(T data) {
        this.data = data;
        return this;
    }
}
